package com.atguigu5.dao;

import com.beans.User;
import com.utils.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import static org.junit.Assert.*;

/**
 * @auther user
 * @date 2021/12/10 - 7:51
 */
public class UserDaoImplTest {
    UserDaoImpl dao = new UserDaoImpl();
    @Test
    public void inert() {
        Connection connection = null;
        try {
            // 获取链接
            connection = JDBCUtils.getConnection();
            // 设置不允许自动提交
            connection.setAutoCommit(false);
            User user = new User(0,"name","password","address","137596148888");
            int count = dao.inert(connection, user);
            if (count == 1){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource1(connection,null,null);
        }
    }

    @Test
    public void deleteById() {
        Connection connection = null;
        try {
            // 获取链接
            connection = JDBCUtils.getConnection();
            // 设置不允许自动提交
            connection.setAutoCommit(false);
            int count = dao.deleteById(connection,6);
            if (count == 1){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource1(connection,null,null);
        }
    }

    @Test
    public void updateById() {
        Connection connection = null;
        try {
            // 获取链接
            connection = JDBCUtils.getConnection();
            // 设置不允许自动提交
            connection.setAutoCommit(false);
            User user = new User(7,"更新后","password","address","137596148888");
            int count = dao.updateById(connection,user);
            if (count == 1){
                System.out.println("更新成功");
            }else {
                System.out.println("更新失败");
            }
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource1(connection,null,null);
        }
    }

    @Test
    public void getUserById() {
        Connection connection = null;
        try {
            // 获取链接
            connection = JDBCUtils.getConnection();
            // 设置不允许自动提交
            connection.setAutoCommit(false);
            User user = dao.getUserById(connection, 6);
            System.out.println("user = " + user);
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource1(connection,null,null);
        }
    }

    @Test
    public void getAll() {
        Connection connection = null;
        try {
            // 获取链接
            connection = JDBCUtils.getConnection();
            // 设置不允许自动提交
            connection.setAutoCommit(false);
            List<User> userList = dao.getAll(connection);
            userList.forEach(System.out::println);
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource1(connection,null,null);
        }
    }

    @Test
    public void getCount() {
        Connection connection = null;
        try {
            // 获取链接
            connection = JDBCUtils.getConnection();
            // 设置不允许自动提交
            connection.setAutoCommit(false);
            Long count = dao.getCount(connection);
            System.out.println("count = " + count);
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            //恢复自动提交
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //关闭资源
            JDBCUtils.closeResource1(connection,null,null);
        }
    }
}